package com.xal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xal.pojo.entiey.Student;
import com.xal.pojo.vo.StudnetVo;
import com.xal.service.StudentService;
import com.xal.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: Swagger
 * @author: 邢奥林
 * @create: 2023-04-12 19:44
 **/
@RestController
@Api(tags = "学生操作API")
@RequestMapping("student")
//@CrossOrigin//解决跨域问题
public class StudentController {
    @Autowired
    private StudentService studentService;



    @PostMapping("find/{current}/{pageSize}")
    @ApiOperation(value = "根据条件查询学生和所在班级")
    private Result findByPage(
            @ApiParam(value = "当前页面", name = "current", required = true, defaultValue = "1")
            @PathVariable Integer current,
            @ApiParam(value = "每页条数", name = "pageSize", required = true, defaultValue = "10")
            @PathVariable Integer pageSize,
            @RequestBody StudnetVo studentVo) {
        Result byPage = studentService.findByPage(current, pageSize, studentVo);
        return byPage;
    }

    @ApiOperation("根据id查询")
    @GetMapping("findById/{sid}")
    public Result findById(@ApiParam(value = "输入id", name = "sid") @PathVariable Integer sid) {
        Result student = studentService.findById(sid);
        return  student;

    }

    @ApiOperation("查询所有学生")
    @GetMapping("findAll")
    public Result findAll() {
        Page<Student> page = new Page<>(1, 5);
        Result all = studentService.findAll();
        return all;

    }

    @ApiOperation(value = "修改学生信息")
    @PutMapping("update")
    public Result update(/*@ApiParam(value = "请输入学生id", name = "sid") @PathVariable Integer sid,
                         @ApiParam(value = "请输入姓名", name = "sname") @RequestBody String sname,
                         @ApiParam(value = "请输入年纪", name = "sage") @PathVariable Integer sage,
                         @ApiParam(value = "请输入班级序号", name = "cid") @PathVariable Integer cid*/
    @RequestBody Student student) {



        Result i = studentService.update(student);
        return i;
    }

    @ApiOperation("删除学生信息")
    @DeleteMapping("delete/{sid}")
    public Result delete(@ApiParam(value = "请输入要删除的学生id", name = "sid") @PathVariable Integer sid) {

        Result i = studentService.delete(sid);
        return i;
    }

    @ApiOperation(value = "添加学生信息")
    @PostMapping("insert")
    private Result insert(
          /*  @ApiParam(value = "请输入姓名", name = "sname") @RequestBody String sname,
            @ApiParam(value = "请输入班级编号", name = "cid") @PathVariable Integer cid,
            @ApiParam(value = "请输入年龄", name = "sage") @PathVariable Integer sage*/
    @RequestBody Student student) {

        Result insert = studentService.insert(student);
        return insert;

    }
    @ApiOperation(value = "多选删除")
    @PostMapping("deletebach")
    public Result deletebach(@RequestBody List<Student> students){
        Result bachdelete = studentService.Bachdelete(students);
        return bachdelete;
    }
}
